 /*
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package spwodne;

import java.awt.Container;
import java.awt.event.*;
import javax.swing.*;
import org.jdesktop.layout.GroupLayout;

public class OESp extends JFrame {

    public OESp(MTSpolek mt) {
        initComponents();
        this.mt = mt;
        nowa = true;
        setTitle("Tworzenie nowej Sp\u0102\u0142\u0139\u201Aki Wodnej");
    }

    public OESp(MTSpolek mt, int numer, String naz, float st) {
        initComponents();
        this.mt = mt;
        nowa = false;
        nr = numer;
        setTitle((new StringBuilder()).append("Edycja Sp\u0102\u0142\u0139\u201Aki ").append(naz).toString());
        jTextField1.setText(naz);
        jTextField2.setText(Float.toString(st));
    }

    private void initComponents() {
        jPanel1 = new JPanel();
        jTextField1 = new JTextField();
        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        jTextField2 = new JTextField();
        jLabel3 = new JLabel();
        jButton1 = new JButton();
        jButton2 = new JButton();
        setDefaultCloseOperation(2);
        setAlwaysOnTop(true);
        setResizable(false);
        jLabel1.setText("Nazwa");
        jLabel2.setText("Skladka");
        jTextField2.setHorizontalAlignment(11);
        jTextField2.setText("0");
        jTextField2.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent evt) {
                jTextField2KeyPressed(evt);
            }
            final OESp this$0;
            

            {
                this$0 = OESp.this;
            //super();
            }
        });
        jLabel3.setText("z\u0142");
        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(1).add(2, jPanel1Layout.createSequentialGroup().addContainerGap().add(jPanel1Layout.createParallelGroup(1).add(jLabel2).add(jLabel1)).addPreferredGap(0).add(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().add(jTextField2, -2, 59, -2).add(26, 26, 26).add(jLabel3)).add(jTextField1, -2, 243, -2)).add(23, 23, 23)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jPanel1Layout.createParallelGroup(3).add(jTextField1, -2, -1, -2).add(jLabel1)).addPreferredGap(0).add(jPanel1Layout.createParallelGroup(3).add(jTextField2, -2, -1, -2).add(jLabel2).add(jLabel3)).addContainerGap(-1, 32767)));
        jButton1.setText("Ok");
        jButton1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
            final OESp this$0;
            

            {
                this$0 = OESp.this;
            //super();
            }
        });
        jButton1.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent evt) {
                jButton1KeyPressed(evt);
            }
            final OESp this$0;
            

            {
                this$0 = OESp.this;
            //super();
            }
        });
        jButton2.setText("anuluj");
        jButton2.setDefaultCapable(false);
        jButton2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
            final OESp this$0;
            

            {
                this$0 = OESp.this;
            //super();
            }
        });
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(1).add(layout.createSequentialGroup().add(jPanel1, -2, -1, -2).addPreferredGap(0, 44, 32767).add(layout.createParallelGroup(2, false).add(jButton1, -1, -1, 32767).add(jButton2, -1, -1, 32767)).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(1).add(layout.createSequentialGroup().addContainerGap().add(layout.createParallelGroup(2).add(layout.createSequentialGroup().add(12, 12, 12).add(jButton1).addPreferredGap(0).add(jButton2)).add(1, jPanel1, -2, -1, -2)).addContainerGap(31, 32767)));
        pack();
    }

    private void jTextField2KeyPressed(KeyEvent evt) {
        int key = evt.getKeyCode();
        KeyEvent _tmp = evt;
        if (key == 10) {
            zakoncz();
        }
    }

    private void jButton1KeyPressed(KeyEvent evt) {
        int key = evt.getKeyCode();
        KeyEvent _tmp = evt;
        if (key == 10) {
            zakoncz();
        }
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        dispose();
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
        zakoncz();
    }

    private void zakoncz() {
        try {
            float ile = Float.parseFloat(jTextField2.getText().replace(",", "."));
            if (nowa) {
                mt.addRow(jTextField1.getText(), ile);
            } else {
                mt.editRow(nr, jTextField1.getText(), ile);
            }
            dispose();
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "B\u0139\u201Aad podczas wprowadzania stawki\n(u\u0139\u013Dywaj kropki do oddzielenia cz\304\u2122\u0139\u203Aci u\u0139\u201Aamkowej)");
        }
    }
    private MTSpolek mt;
    private boolean nowa;
    private int nr;
    private JButton jButton1;
    private JButton jButton2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JPanel jPanel1;
    private JTextField jTextField1;
    private JTextField jTextField2;
}
